<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="资源名称">
            <el-input
              v-model="search.name"
              placeholder="请输入资源名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="资源路径">
            <el-input
              v-model="search.url"
              placeholder="请输入资源路径"
            ></el-input>
          </el-form-item>
          <el-form-item label="资源分类">
            <el-select
              v-model="search.categoryId"
              clearable
              placeholder="请选择分类"
            >
              <el-option
                v-for="item in categoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onSearch">查询</el-button>
          </el-form-item>
        </el-form>
        <el-row>
          <el-button @click="handleCreate">添加</el-button>
          <el-button @click="$router.push({ name: 'resource-category' })"
            >资源分类</el-button
          >
        </el-row>
      </div>
      <el-table v-loading="loading" :data="resourceList" style="width: 100%">
        <el-table-column type="index" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="name" label="资源名称" align="center">
        </el-table-column>
        <el-table-column
          prop="url"
          label="资源路径"
          align="center"
          min-width="150"
        >
        </el-table-column>
        <el-table-column
          prop="categoryId"
          label="分类ID"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="description" align="center" label="资源描述">
        </el-table-column>
        <el-table-column prop="updatedBy" align="center" label="更新人员">
        </el-table-column>
        <el-table-column
          prop="updatedTime"
          align="center"
          label="更新时间"
          min-width="100"
        >
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :disabled="loading"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="search.current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="search.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </el-card>
    <create-or-update-resource
      :isEdit="isEditResource"
      :show="dialogVisible"
      :resource="resource"
      :categoryList="categoryList"
      @changeDialog="changeDialog"
    ></create-or-update-resource>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import dayjs from 'dayjs'
import {
  searchResource,
  getResourceCategory,
  deleteResource
} from '@/apis/resource'
import CreateOrUpdateResource from './create-or-update-resource.vue'
export default Vue.extend({
  name: 'resource-list',
  components: {
    CreateOrUpdateResource
  },
  data() {
    return {
      categoryList: [],
      resourceList: [],
      search: {
        size: 15,
        current: 1,
        name: '',
        categoryId: null,
        url: ''
      },
      pagination: {
        total: 0
      },
      dialogVisible: false,
      isEditResource: false,
      resource: {},
      loading: false
    }
  },
  created() {
    this.loadResource()
    this.loadCategory()
  },
  methods: {
    async loadResource() {
      this.loading = true
      const { data } = await searchResource(this.search)
      if (data.code === '000000') {
        this.resourceList = data.data.records.map((record: any) => ({
          id: record.id,
          name: record.name,
          url: record.url,
          description: record.description,
          updatedBy: record.updatedBy,
          updatedTime: dayjs(record.createTime).format('YYYY-MM-DD HH:MM:ss'),
          categoryId: record.categoryId
        }))
        this.pagination.total = data.data.total
      }
      this.loading = false
    },
    async loadCategory() {
      const { data } = await getResourceCategory()
      if (data.code === '000000') {
        this.categoryList = data.data.map((item: any) => ({
          name: item.name,
          id: item.id
        }))
      }
    },
    onSearch() {
      this.search.current = 1
      this.loadResource()
    },
    handleSizeChange(val: number) {
      this.search.size = val
      this.search.current = 1
      this.loadResource()
    },
    handleCurrentChange(val: number) {
      this.search.current = val
      this.loadResource()
    },
    handleCreate() {
      this.isEditResource = false
      this.resource = {}
      this.changeDialog(true)
    },
    handleEdit(row: any) {
      this.isEditResource = true
      this.resource = {
        id: row.id,
        name: row.name,
        url: row.url,
        description: row.description,
        categoryId: row.categoryId
      }
      this.changeDialog(true)
    },
    handleDelete(row: any) {
      this.$confirm('是否要删除该资源？', '删除资源', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async() => {
          const { data } = await deleteResource(row.categoryId)
          if (data.code === '000000') {
            this.$message.success('删除资源成功')
            this.loadResource()
          }
        })
        .catch(() => {
          this.$message.info('取消删除资源')
        })
    },
    changeDialog(state: boolean, reload = false) {
      this.dialogVisible = state
      if (reload) this.loadResource()
    }
  }
})
</script>
<style lang="scss" scoped>
</style>
